package com.example.s05datajpaonetomany.mymodel;

import com.example.s05datajpaonetomany.model.OrderItem;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

//两个表中，可以通过部门编号建立一对多的关系，一个部门可以对应多个员工。

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "departments")
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "dept_name")
    private String deptName;


    // 一对多单向映射
    // OneToMany的默认获取类型:LAZY
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "dept_items", referencedColumnName = "id")
    private Set<Employee> deptItems = new HashSet<>();

    //添加部门id
    public void add(Employee employee) {
        if (employee != null) {
            if (deptItems == null) {
                deptItems = new HashSet<>();
            }

            deptItems.add(employee);
            // item.setOrder(this);
        }
    }
}
